package com.financing.service.invest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.financing.model.invest.ShareBenefit;
import com.financing.service.BaseService;
import com.jfinal.plugin.activerecord.Page;

public class ShareBenefitService extends BaseService<ShareBenefit>{

	/**
	 * 分页数据
	 */
	public Page<ShareBenefit> list(int page, int size, Map<String, String> map) {
		return ShareBenefit.me.paginate(page, size, buildSql("share_benefit",Arrays.asList("real_name", "create_time", "user_id", "agent_id"), map));
	}
	
	/**
	 * 获取用户数据
	 */
	public List<ShareBenefit> findByTotalGroup(int userId) {
		return ShareBenefit.me.find("select user_id, real_name, sum(trade_amount) as total_trade_amount, sum(amount) as total_amount FROM share_benefit where agent_id=? AND type='group' GROUP BY user_id ORDER BY create_time DESC ", userId);
	}
	
	/**
	 * 获取用户数据
	 */
	public ShareBenefit findByGroup(int agentId, int userId, String start, String end) {
		return ShareBenefit.me.findFirst("select user_id, real_name, sum(trade_amount) as total_trade_amount, sum(amount) as total_amount FROM share_benefit where agent_id=? AND user_id=? AND type='group' AND create_time>=?  AND create_time<=? GROUP BY user_id ORDER BY create_time DESC ", agentId,userId,start,end);
	}

	/** 
	 * 获取用户总收益
	 */
	public double sumByUser(int userId) {
		ShareBenefit s = ShareBenefit.me.findFirst("select SUM(amount) as total from share_benefit where agent_id=?",userId);
		return s.getDouble("total")!=null && s.getDouble("total")>0 ? s.getDouble("total") : 0;
	}
}
